// pages/shop/detail.js
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 10,
    uid: 9,
    entrance: '',
    current_no: 1,
    if_show_ticket_box: false,
    if_show_mask: false,
    ticket_list: [],
    more_list: [],
    shop_id: 0,
    button_list: [
      {
        title: '电话',
        img: '/images/mobile_black.png'
      },
      {
        title: '约2094.1km',
        img: '/images/location_gray.png'
      },
      {
        title: '分享',
        img: '/images/share.png'
      },
      {
        title: '收藏',
        img: '/images/ic_collect_sel.png'
      },

    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init_data(options)
    this.get_shop_detail()
    this.get_ticket_list()
    this.get_more_list()
  },

  init_data(options) {
    this.setData({
      uid: util.getAppValue(App.globalData.uid),
      shop_id: options.id,
      entrance: options.entrance
    })
  },

  get_more_list() {
    let params = {
      key: this.data.keyword_value,
      uid: this.data.uid,
      shop_id: this.data.shop_id,
    }
    util.showLoading('加载中')
    request.post(api.goods_list, params).then(res => {
      util.hideLoading()
      this.setData({
        more_list: res,
      })
    })
  },
  // 优惠券
  get_ticket_list() {
    let params = {
      uid: this.data.uid,
      pageSize: 3,

    }
    request.post(api.ticketlist, params).then(res => {
      this.setData({
        ticket_list: res
      })
    })
  },
  get_shop_detail() {
    let params = {
      id: this.data.shop_id,
    }
    request.post(api.shopdetail, params).then(res => {
      this.setData({
        detail_data: res
      })
      // this.get_detail_image(res.content)
      // this.load_acode()
      if (this.data.entrance == 'promotion') {
        this.count_time(res.ext_skill.end_time)
      }
    })
  },

  swiper_no_change(e) {
    this.setData({
      current_no: e.detail.current + 1
    })
  },
  show_ticket() {
    this.setData({
      if_show_ticket_box: true,
      if_show_mask: true
    })
  },
  clear_mask() {
    this.setData({
      if_show_mask: false,
      if_show_ticket_box: false,
    })
  },
  add_ticket(e) {
    let tid = e.currentTarget.dataset.tid
    let params = {
      uid: this.data.uid,
      type: 2,
      tid,
      // page: 1,
      // pageSize: 10,
    }
    request.post(api.ticketadd, params).then(res => {
      console.log(res);
      util.showToast("领取成功")
    })
  },
  go_settling() {
    wx.navigateTo({
      url: '/pages/shop/settling',
    })
  },
  go_detail_from_component(e) {
    let id = e.detail.currentTarget.dataset.id
    if (!id) {
      id = 10
    }
    wx.navigateTo({
      url: '/pages/good/detail?id=' + id,
    })
  },
  cart_add() {
    util.showLoading("正在添加")
    let params = {
      // goods_num: this.data.cart_num,
      goods_id: this.data.goods_id,
      uid: this.data.uid
    }
    request.post(api.cart_add, params).then(res => {
      this.setData({
        if_show_box: false
      })
      util.showToast("已加入购物车")
    })
  },
  active_button(e) {
    let title = e.currentTarget.dataset.title
    if (title == '电话') {
      this.show_phone()
    }
  },
  show_phone() {
    wx.showModal({
      title: "电话号码",
      content: this.data.detail_data.phone,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})